﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using System.Linq;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Helpers;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.Share;
using System.Collections;

namespace ASPNETVN.PORTAL.Modules.FEEDBACK.Controls
{
    public partial class ItemManager : ModulePage //AddEditItemControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            this.BuildCategory();

            if (IsPostBack == false)
            {
                gvFeedback.Sort("CreatedDate", SortDirection.Descending);
            }
        }

        private void BuildCategory()
        {
            Hashtable settings = this.Settings;
            Guid moduleID = this.ModuleID; // new Guid(settings["FEEDBACK.ModuleID"].ToString());
            if (moduleID == Guid.Empty)
            {
                ContentPane.Controls.Add(new LiteralControl("Chưa cấu hình FEEDBACK.ModuleID"));
                return;
            }

            // Left Pane.
            LiteralControl literal = new LiteralControl("");
            literal.Text += "<div class='MenuLink'>";

            IList<Category> allCategories = Category.SelectCollectionAll(moduleID);
            IList<Category> topCategories = allCategories.Where(x => x.ParentID == 0).OrderBy(x => x.SortOrder).ToList();
            string url = string.Empty;
            foreach (Category topCategory in topCategories)
            {
                url = HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.ItemManager, "categoryid={0}", topCategory.ID);
                url = url.Substring(2);
                literal.Text += string.Format("<div class='MenuLinkGroup'><a href='{0}'>{1}</a></div>", url, topCategory.Title);
                Category category = topCategory;
                IList<Category> childCategories = allCategories.Where(x => x.ParentID == category.ID).OrderBy(x => x.SortOrder).ToList();

                foreach (Category childCategory in childCategories)
                {
                    url = HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.ItemManager, "categoryid={0}", childCategory.ID);
                    url = url.Substring(2);

                    literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» {1}</a>", url, childCategory.Title);
                }
            }
            literal.Text += "</div>";
            ContentPane.Controls.Add(literal);
        }

        protected void gvFeedback_SelectedIndexChanged(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, Convert.ToInt32(gvFeedback.SelectedValue), "FeedbackDetail");
            Response.Redirect(url);
        }
    }
}